// 用户ID
let id = getUrlParam("id");
$(function () {
    GetUserRole(); 
    // loadFnc();
});
//获取用户权限
function GetUserRole() {
    // let thisdata = [];
    // thisdata.push(InitRole(id));
    let USERdata = InitRole(id);
    if (USERdata != null) {
        loadFnc();
    }
    else OpenErrmsg("web/errhtml/404.html");
}
function loadFnc() {
    InitCombox();
    InitData();
}
var total = ''
let path = getDataPath();
var listdata = []
// 加载列表数据
function getlistdata(pageNumber, pageSize) {
    let url = ''
    url = path + '/ls172/data-analysis/getMainDataThirdOneList'
    var params = {
        "pageNum": pageNumber,
        "pageSize": pageSize,
    }
    $.ajax({
        url: url,
        type: "post",
        async: false,
        dataType: "json",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json"
        },
        data: JSON.stringify(params),

        success: function (data) {
            listdata = data.data.records
            total = data.data.total
        },
        error: function (data) {
            console.log("code", data);
        }
    });
}
// 初始化列表数据
function InitData() {
    getlistdata(1, 10)
    $('#dg_DateList').datagrid({
        data: listdata
    });
    $('#dg_pagination').pagination({
        total: total,
        onSelectPage: function (pageNumber, pageSize) {
            getlistdata(pageNumber, pageSize)
            $('#dg_DateList').datagrid({
                data: listdata
            });
        },
        onRefresh: function (pageNumber, pageSize) {
            $('#dg_pagination').pagination('refresh', {	// 改变选项并刷新分页栏信息
                total: total,
                pageNumber: 1
            });
            getlistdata(1, 10)
            $('#dg_DateList').datagrid({
                data: listdata
            });
        }
    });

}
// 选择框
var SearchTypeA = ''
var SearchTypeB = ''
var SearchTypeXD = ''
setInterval(() => {
    InitChartYSJ_A(SearchTypeA);
    InitChartYSJ_B(SearchTypeB)
    InitChart_XD(SearchTypeXD)
}, 5000);
function InitCombox() {
    $('#cmb_SearchTypeA').combobox({
        onSelect: function (rec) {
            InitChartYSJ_A(rec.value);
            SearchTypeA = rec.value
        },
    });
    $('#cmb_SearchTypeB').combobox({
        onSelect: function (rec) {
            InitChartYSJ_B(rec.value);
            SearchTypeB = rec.value
        },
    });
    $('#cmb_SearchType_XD').combobox({
        onSelect: function (rec) {
            InitChart_XD(rec.value);
            SearchTypeXD = rec.value
        },
    });
}
// 请求图表下端数据
var CompressorPosition = ''
function getinfodata(position, type, equiment) {
    var type = type
    let url = ''
    url = path + '/ls172/data-analysis/getMainDataThreeInfo'
    $.ajax({
        url: url,
        async: false,
        type: "GET",
        success: function (data) {
            if (equiment == 'A') {
                if (type == 'temp_E') {
                    position.innerText = '最大值：' + data.data.CompressorA_I_T[0].value.toFixed(2) + '℃；' + '最小值：' + data.data.CompressorA_I_T[1].value.toFixed(2) + '℃；' + '均值：' + data.data.CompressorA_I_T[2].value.toFixed(2) + '℃'
                } else if (type == 'temp_O') {
                    position.innerText = '最大值：' + data.data.CompressorA_O_T[0].value.toFixed(2) + '℃；' + '最小值：' + data.data.CompressorA_O_T[1].value.toFixed(2) + '℃；' + '均值：' + data.data.CompressorA_O_T[2].value.toFixed(2) + '℃'
                } else {
                    position.innerText = '最大值：' + data.data.CompressorA_P[0].value.toFixed(2) + 'Kpag；' + '最小值：' + data.data.CompressorA_P[1].value.toFixed(2) + 'Kpag；' + '均值：' + data.data.CompressorA_P[2].value.toFixed(2) + 'Kpag'
                }
            } else if (equiment == 'B') {
                if (type == 'temp_E') {
                    position.innerText = '最大值：' + data.data.CompressorB_I_T[0].value.toFixed(2) + '℃；' + '最小值：' + data.data.CompressorB_I_T[1].value.toFixed(2) + '℃；' + '均值：' + data.data.CompressorB_I_T[2].value.toFixed(2) + '℃'
                } else if (type == 'temp_O') {
                    position.innerText = '最大值：' + data.data.CompressorB_O_T[0].value.toFixed(2) + '℃；' + '最小值：' + data.data.CompressorB_O_T[1].value.toFixed(2) + '℃；' + '均值：' + data.data.CompressorB_O_T[2].value.toFixed(2) + '℃'
                } else {
                    position.innerText = '最大值：' + data.data.CompressorB_P[0].value.toFixed(2) + 'Kpag；' + '最小值：' + data.data.CompressorB_P[1].value.toFixed(2) + 'Kpag；' + '均值：' + data.data.CompressorB_P[2].value.toFixed(2) + 'Kpag'
                }
            } else if (equiment == 'Scrubber') {
                if (type == 'Liquid_L') {
                    position.innerText = '最大值：' + data.data.Scrubber_L[0].value.toFixed(2) + 'L；' + '最小值：' + data.data.Scrubber_L[1].value.toFixed(2) + 'L；' + '均值：' + data.data.Scrubber_L[2].value.toFixed(2) + 'L'
                } else {
                    position.innerText = '最大值：' + data.data.Scrubber_P[0].value.toFixed(2) + 'Kpag；' + '最小值：' + data.data.Scrubber_P[1].value.toFixed(2) + 'Kpag；' + '均值：' + data.data.Scrubber_P[2].value.toFixed(2) + 'Kpag'
                }
            } 
        },
        error: function (data) {
            console.log("code", data);
        }
    })
}
// 分类获取
function getAdata(type, equiment) {
    if (equiment == 'A') {
        CompressorPosition = document.getElementById('CompressorA')
    } else if (equiment == 'B') {
        CompressorPosition = document.getElementById('CompressorB')
    } else if (equiment == 'Scrubber') {
        CompressorPosition = document.getElementById('Scrubber')
    }
    getinfodata(CompressorPosition, type, equiment)
}
// 请求折线图数据
function getChartData(Type, position) {
    let xAxis = [];
    let data1 = [];
    let data2 = [];
    let data3 = [];
    let url = ''
    url = path + '/ls172/data-analysis/mainDataLineChartCTem'
    $.ajax({
        url: url,
        async: false,
        type: "GET",
        success: function (data) {
            xAxis = data.data.time
            if (position == 'A') {
                if (Type == 'temp_E') {
                    data1 = data.data.data[0].vals
                } else if (Type == 'temp_O') {
                    data1 = data.data.data[1].vals
                } else {
                    data1 = data.data.data[2].vals
                }
                createChart("chartdiv1", xAxis, data1);
            } else if (position == 'B') {
                if (Type == 'temp_E') {
                    data2 = data.data.data[4].vals
                } else if (Type == 'temp_O') {
                    data2 = data.data.data[5].vals
                } else {
                    data2 = data.data.data[7].vals
                }
                createChart("chartdiv2", xAxis, data2);
            } else if (position == 'Scrubber') {
                if (Type == 'Liquid_L') {
                    data3 = data.data.data[8].vals
                } else if (Type == 'press_O') {
                    data3 = data.data.data[10].vals
                }
                createChart("chartdiv3", xAxis, data3);
            } 
        },
        error: function (data) {
            console.log("code", data);
        }
    })
}
//压缩机A
function InitChartYSJ_A(Type) {
    if (Type == "temp_E") {
        getAdata(Type, 'A')
        getChartData(Type, 'A')
    } else if (Type == "temp_O") {
        getAdata(Type, 'A')
        getChartData(Type, 'A')
    } else if (Type == "press_O") {
        getAdata(Type, 'A')
        getChartData(Type, 'A')
    }

}
//压缩机B
function InitChartYSJ_B(Type) {
    if (Type == "temp_E") {
        getAdata(Type, 'B')
        getChartData(Type, 'B')
    } else if (Type == "temp_O") {
        getAdata(Type, 'B')
        getChartData(Type, 'B')
    } else if (Type == "press_O") {
        getAdata(Type, 'B')
        getChartData(Type, 'B')
    }

}
//洗涤器
function InitChart_XD(Type) {
    if (Type == "Liquid_L") {
        getAdata(Type, 'Scrubber')
        getChartData(Type, 'Scrubber')
    } else if (Type == "press_O") {
        getAdata(Type, 'Scrubber')
        getChartData(Type, 'Scrubber')
    }

}
function createChart(id, xAxis, data) {
    let myChart = echarts.init(document.getElementById(id));
    let newxAxis = []
    for (let i = 0; i < xAxis.length; i++) {
        newxAxis.push(xAxis[i].substr(10, 18))
    }
    let option = {
        title: {

        },
        tooltip: {
            trigger: 'axis'
        },
        // legend: {
        //     data: '温度',
        // },
        grid: {
            top: '6%',
            left: '3%',
            right: '4%',
            bottom: '5%',
            containLabel: true
        },
        toolbox: {
            // feature: {
            //     saveAsImage: {}
            // }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: newxAxis
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: data,
                type: 'line'
            }
        ]
    }
    myChart.setOption(option);
}